﻿using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;
using SCG.SolarVengeanceEngine.BrainInfo;

namespace SCG.SolarVengeance.Brains
{
    public class AliceH3 : Brain
    {
        Boolean runOnce = true;
        //Boolean ohno;
        //int i, distance;
        StarSystemInfo sys, mine;

        public override ChallengeLevel Challenge
        {
            get
            {
                return ChallengeLevel.Difficult;
            }
        }

        public static StarSystemInfo EnemyCapital = null;

        public override string Author
        {
            get { return "Starz2far"; }
        }
        public override List<StarShipType> AvailableShipTypes
        {
            get
            {
                List<StarShipType> shipTypes = new List<StarShipType>();
                shipTypes.Add(StarShipType.WarShip);
                shipTypes.Add(StarShipType.HijackShip);
                shipTypes.Add(StarShipType.Drone);
                return shipTypes;
            }
        }

        public override void Execute()
        {
            if (runOnce)
            {
                foreach (StarSystemInfo s in StarSystems)
                {
                    if (s.IsCapital && !s.OwnedByMe)
                    {
                        EnemyCapital = s;
                    }
                }
                if (EnemyCapital != null)
                {
                    //ohno = true;
                    BuildStarShip(Capital, StarShipType.HijackShip, 10, 0, false);
                    BuildStarShip(Capital, StarShipType.HijackShip, 10, 0, false);
                    BuildStarShip(Capital, StarShipType.HijackShip, 10, 0, false);
                    BuildStarShip(Capital, StarShipType.WarShip, 11, 11, false);
                    SetAutoStarShipType(Capital, StarShipType.HijackShip, 2, 0, false);
                    SetAutoStarShip(Capital, true);
                }
                runOnce = false;
            }
            foreach (StarSystemInfo s in StarSystems)
            {
                StarShipInfo tooclose = ClosestStarShip(s.X, s.Y, 4, FindShipFlags.Enemy, ShipKnownFlags.Either);
                if (s.IsCapital && !s.OwnedByMe)
                {
                    EnemyCapital = s;
                }
                if (tooclose != null) 
                {
                    BuildStarShip(s, StarShipType.HijackShip, 2, 0, false);
                    SetAutoStarShipType(s, StarShipType.Drone, 2, 0, false);
                    SetAutoStarShip(s, true);
                }
                else if (s.Equals(Capital))
                {
                    if (s.Shields < 10)
                    {
                        BuildShields(s, 10);
                    }
                    SetAutoStarShipType(s, StarShipType.WarShip, 7, Random(4,5), false);
                    SetAutoStarShip(s, true);
                }
                else
                {
                    if ((Random(0, StarSystems.Count) > MyStarShips.Count) || ((StarSystems.Count * 10) < MyStarShips.Count))
                    {
                        SetAutoStarShipType(s, StarShipType.WarShip, Random(6,6), Random(2,4), false);
                        SetAutoStarShip(s, true);
                    }
                    else
                    {
                        SetAutoStarShipType(s, StarShipType.HijackShip, Random(6,6), 0, false);
                        SetAutoStarShip(s, true);
                    }
                }
            }

            foreach (StarShipInfo sh in MyStarShips)
            {
                mine = ClosestStarSystem(sh.X, sh.Y, Int32.MaxValue, FindSystemFlags.All);
                sys = ClosestStarSystem(sh.X, sh.Y, Int32.MaxValue, FindSystemFlags.Unfriendly);
                 
                if (sys != mine && !sys.IsCapital)
                    AttackAdjacent(sh, false);
                else
                    AttackAdjacent(sh, true);

                if (sh.ShipType.Equals(StarShipType.TechShip))
                {
                    if (Capital.X < (MapWidth / 2))
                        Move(sh, 0, 0);
                    else
                        Move(sh, MapWidth, MapHeight);
                }
                else if (sh.ShipType.Equals(StarShipType.StarTillery))
                {
                    Move(sh, sys.X + 5, sys.Y + Random(-10, 10));
                }
                else if (sh.ShipType.Equals(StarShipType.HijackShip) || sh.ShipType.Equals(StarShipType.Drone))
                {
                    StarShipInfo ship = ClosestStarShip(sh.X, sh.Y, Int32.MaxValue, FindShipFlags.Enemy, ShipKnownFlags.Either);
                    Move(sh, sys.X, sys.Y + Random(-5,5));
                    if (ship != null)
                        Target(sh, ship);
                }
                else if (EnemyCapital != null)
                {
                    AttackAdjacent(sh, false);
                    Move(sh, EnemyCapital.X, EnemyCapital.Y);
                    Target(sh, EnemyCapital);
                }
                else
                {
                    Move(sh, sys.X, sys.Y);
                    if (sh.EffectiveWeapons != 1 || sh.EffectiveWeapons > sys.Shields)
                        Target(sh, sys);
                }
            }
        }

        public override string Description
        {
            get
            {
                return "The third version of AliceH. Utilizes only " +
                    "three starship types: WarShip, Drone and HijackShip. WarShips and Drones pack a " +
                    "consistent, economic punch allowing it to expand effectively and " +
                    "quickly. HijackShips enable it to compete against brains with a " +
                    "variety of StarShip types by stealing powerful StarShips at a cheaper " +
                    "cost than it took the enemy to build them. Unlike the previous " +
                    "version, AliceH3 recognizes if the enemy Capital is unusually near it " +
                    "in the beginning and reacts appropriately.";
            }
        }
    }
}
